import React from 'react';
import {Table, Form, Card} from 'antd';
import SearchForm from '../../components/AutoForm/SearchForm';
import {connect} from "dva";
import moment from "moment";

@connect(({carOil, loading}) => ({
  tableLoading: loading.effects['oil/page'],
  tableData: carOil.pageList,
  pagination: carOil.pagination,
  columns: carOil.columns,
  searchFields: carOil.searchFields,
  searchValues: carOil.searchValues,
  selectionIds: carOil.selectionIds,
  selectedRows: carOil.selectedRows
}))
class EditableTable extends React.Component {

  componentDidMount() {
    this.handleSearch();
  }

  handleSearchChange = (fields) => {
    const {dispatch} = this.props;
    dispatch({
      type: 'carOil/handleSearchChange',
      payload: fields
    })
  }

  handleSearch = (flag) => {
    const {dispatch, pagination, searchValues} = this.props;
    let pageParam = {pageSize: pagination.pageSize, current: flag==='btn' ? 1 : pagination.current}
    let searchParam = {};
    Object.keys(searchValues).map((key) => {
      if (key === 'createTimeArray') {
        const dateArray = searchValues[key]['value'];
        if (dateArray && dateArray.length) {
          let startTime = moment(dateArray[0]);
          let endTime = moment(dateArray[1]);
          searchParam[key] = [];
          searchParam[key].push(startTime.format('YYYY-MM-DD HH:mm:ss'));
          searchParam[key].push(endTime.format('YYYY-MM-DD HH:mm:ss'));
        }
      }else{
        searchParam[key] = searchValues[key]['value'];
      }
    })
    const param = {
      ...searchParam,
      ...pageParam
    };
    dispatch({
      type: 'carOil/page',
      payload: param
    })
  }

  render() {
    const {
      columns,
      pagination,
      tableLoading,
      tableData,
      searchFields,
      searchValues,
      dispatch
    } = this.props;

    pagination.onChange = (page, pageSize) => {
      let searchParam = {};
      Object.keys(searchValues).map((key) => {
        searchParam[key] = searchValues[key]['value'];
      })
      dispatch({
        type: 'carOil/page',
        payload: {
          ...searchParam,
          current: page,
          pageSize
        }
      })
    }
    pagination.onShowSizeChange = (current, size) => {
      let searchParam = {};
      Object.keys(searchValues).map((key) => {
        searchParam[key] = searchValues[key]['value'];
      })
      dispatch({
        type: 'carOil/page',
        payload: {
          ...searchParam,
          current: current,
          pageSize: size
        }
      })
    }


    return (
      <Card bodyStyle={{padding: 0}}>
        <SearchForm
          header="加油记录->操作"
          layout="inline"
          onFieldsChange={this.handleSearchChange}
          onSubmit={() => {this.handleSearch('btn')}}
          fields={searchFields}
          fieldValue={searchValues}
        />
          <Table
            bordered
            rowKey="id"
            loading={tableLoading}
            dataSource={tableData}
            columns={columns}
            scroll={{x: 'max-content'}}
            size="small"
            pagination={pagination}
          />
      </Card>
    );
  }
}

const EditableFormTable = Form.create()(EditableTable);
export default EditableFormTable;
